#include <bits/stdc++.h>
using namespace std;

int g[10001], n, k, dp[10001][1001], m[10001][1001];

int main()
{
    memset(dp, -0x3f, sizeof dp);
    cin >> n >> k;
    for (int i = 1; i <= n; i++)
        cin >> g[i];
    dp[1][1] = g[1], m[1][1] = g[1];
    for (int i = 2; i <= n; i++) {
        for (int j = 1; j < k; j++) {
            if (g[i] > m[i - 1][j] && dp[i][j + 1] < dp[i - 1][j] + (g[i] - m[i - 1][j]) * j + g[i])
                m[i][j + 1] = g[i], dp[i][j + 1] = dp[i - 1][j] + (g[i] - m[i - 1][j]) * j + g[i];
            else if (g[i] <= m[i - 1][j] && dp[i][j + 1] < dp[i - 1][j] + m[i - 1][j])
                m[i][j + 1] = m[i - 1][j], dp[i][j + 1] = dp[i - 1][j] + m[i - 1][j];
            dp[i][1] = max(dp[i][1], dp[i - 1][j] + g[i]);
        }
        dp[i][1] = max(dp[i][1], dp[i - 1][k] + g[i]);
        m[i][1] = g[i];
    }
    int ans = 0;
    for (int i = 1; i <= k; i++)
        ans = max(ans, dp[n][i]);
    cout << ans << endl;
    return 0;
}